package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询护理项目列表
     *
     * @param name     护理项目名称
     * @param status   状态：0-禁用，1-启用
     * @param pageNum  当前页码
     * @param pageSize 每页显示数量
     * @return 护理项目列表
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //1. 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        //2. 调用mapper接口查询数据
        Page<NursingProject> page = nursingProjectMapper.selectByPage(name, status);

        //3. 封装返回的结果

        return PageResponse.of(page, NursingProjectVo.class);
    }

    /**
     * 新增
     *
     * @param nursingProjectDto
     */
    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setCreateTime(new Date());
        nursingProject.setUpdateTime(new Date());

        nursingProjectMapper.insertNursingProject(nursingProject);
    }

    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        return nursingProjectMapper.selectNursingProjectById(id);
    }

    /**
     * 根据id修改数据
     *
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {

        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);

        nursingProjectMapper.updateNursingProject(nursingProject);
    }
}
